package com.yjj.im.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yjj.im.R;


/**
 * Created by n on 2018/7/1.
 */

public class DialogUtil {
    private ProgressDialog dialog = null;
    private static DialogUtil _instance;

    private Dialog _progressDialog;
    private Context context;
    public static synchronized DialogUtil getInstance() {
        if (_instance == null) {
            _instance = new DialogUtil();
        }
        return _instance;
    }

    public void show(Context c, String message, View view) {
        try {
            if (isShow()) {
                close();
            }
            context = c;
            _progressDialog = createLoadingDialog(c, message,view);
            if (!_progressDialog.isShowing()) {
                _progressDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean isShow() {
        if (_progressDialog != null && _progressDialog.isShowing()) {
            return true;
        }
        return false;
    }


    public void showNoTouch(Context c, String message) {
        try {
            _progressDialog = createLoadingDialog(c, message,null);
            _progressDialog.setCancelable(false);
            _progressDialog.setCanceledOnTouchOutside(false);
            _progressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeDialog() {
        getInstance().close();
    }

    // 关闭
    public void close() {
        if (_progressDialog != null && _progressDialog.isShowing()) {
            if(context != null && context instanceof Activity){
                if(!((Activity) context).isFinishing()){
                    _progressDialog.cancel();
                    _progressDialog = null;
                }
            }

        }
    }


    /**
     * 得到自定义的progressDialog
     *
     * @param context
     * @param msg
     * @return
     */
    public static Dialog createLoadingDialog(Context context, String msg, View view) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_layout, null);// 得到加载view
        LinearLayout layout = v.findViewById(R.id.dialog_view);// 加载布局

        TextView tipTextView = v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context);
        loadingDialog.setCanceledOnTouchOutside(false);
        //loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        if(view == null){
            loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        }else {
            loadingDialog.setContentView(view, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        }

        return loadingDialog;

    }
    public static void showLoadDataDialog(Context context, String info, View view) {
        DialogUtil.getInstance().show(context, info,view);
    }
    public static void showLoadDataDialog(Context context) {
        DialogUtil.getInstance().show(context, "",null);
    }

    public static void showLoadDataDialog(Context context, View view) {
        DialogUtil.getInstance().show(context, "正在处理, 请稍后..",view);
    }
}
